package com.huacai.orders.service.impl;

import java.util.Date;
import java.util.List;

import com.huacai.common.utils.DateUtils;
import com.huacai.common.utils.uuid.IdUtils;
import com.huacai.orders.domain.OrderDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huacai.orders.mapper.OrdersMapper;
import com.huacai.orders.domain.Orders;
import com.huacai.orders.service.IOrdersService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

import static com.huacai.common.utils.SecurityUtils.getUserId;

/**
 * 酒店订单Service业务层处理
 *
 * @author hsj
 * @date 2025-05-18
 */
@Service
public class OrdersServiceImpl implements IOrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询酒店订单
     *
     * @param orderId 酒店订单主键
     * @return 酒店订单
     */
    @Override
    public Orders selectOrdersByOrderId(String orderId) {
        return ordersMapper.selectOrdersByOrderId(orderId);
    }

    /**
     * 查询酒店订单列表
     *
     * @param orders 酒店订单
     * @return 酒店订单
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders) {
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 查询酒店订单列表
     *
     * @param orders 酒店订单
     * @return 酒店订单
     */
    @Override
    public List<Orders> selectOrdersListByUserId(Orders orders,Long userId) {
        return ordersMapper.selectOrdersListByUserId(orders, userId);
    }

    /**
     * 查询酒店订单详情
     *
     * @param orderId 酒店订单id
     * @return 酒店订单集合
     */
    @Override
    public List<OrderDetail> selectOrderDetail(String orderId, Long userId){
        return ordersMapper.selectOrderDetail(orderId,userId);
    }

    /**
     * 新增酒店订单
     *
     * @param orders 酒店订单
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders) {
        orders.setOrderId(IdUtils.fastSimpleUUID());
        orders.setUserId(Math.toIntExact(getUserId()));
        orders.setCreateTime(DateUtils.getNowDate());
        return ordersMapper.insertOrders(orders);
    }

    /**
     * 批量新增酒店订单
     *
     * @param orderss 酒店订单List
     * @return 结果
     */
    @Override
    public int batchInsertOrders(List<Orders> orderss) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(orderss)) {
            try {
                for (int i = 0; i < orderss.size(); i++) {
                    int row = ordersMapper.insertOrders(orderss.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == orderss.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改酒店订单
     *
     * @param orders 酒店订单
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders) {
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除酒店订单
     *
     * @param orderIds 需要删除的酒店订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByOrderIds(String[] orderIds) {
        return ordersMapper.deleteOrdersByOrderIds(orderIds);
    }

    /**
     * 删除酒店订单信息
     *
     * @param orderId 酒店订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByOrderId(String orderId) {
        return ordersMapper.deleteOrdersByOrderId(orderId);
    }
}
